#include "stalkmodel.h"

StalkModel* StalkModel::__instance = 0;

StalkModel::StalkModel()
{
    qDebug() << "Creating StalkModel";


//    this->outputFile.setFileName("/home/anderson/test.raw");
//    this->outputFile.open(QIODevice::WriteOnly | QIODevice::Truncate);

    this->timer = new QElapsedTimer();
    this->timer->start();

    this->audioStreamDecoder = new AudioStreamDecoder();
    this->audioStreamDecoder->start();

//    connect(this->net, SIGNAL(onAuthSuccess()), this, SLOT((authSuccess()));
//    connect(this->net, SIGNAL(onAuthFail()), this, SLOT(authFail()));
}

void StalkModel::connectToServer()
{
    emit connectingSignal();
    this->net = new NetworkProtocol();
    connect(this->net, SIGNAL(onConnect()), this, SLOT(connected()));
    connect(this->net, SIGNAL(onError()), this, SLOT(error()));
    connect(this->net, SIGNAL(onGotPacket(NetworkPacket*)), this, SLOT(gotPacket(NetworkPacket*)));
    connect(this->net, SIGNAL(onDisconnect()), this, SLOT(disconnected()));
    connect(this->net, SIGNAL(onConnectionFailed()), this, SLOT(connectionFailed()));
//    QThread *newThread = new QThread();
//    this->net->moveToThread(newThread);
    this->net->doRun();
}

void StalkModel::connected()
{
    emit connectedSignal();
    this->net->sendPacket((new NetworkPacket(1001))->setParam("login", this->login)->setParam("password", this->password));
}

void StalkModel::error()
{
    emit errorSignal();
}

void StalkModel::connectionFailed()
{
    this->net = NULL;
    emit connectionFailedSignal();
}

void StalkModel::disconnected()
{
    emit disconnectedSignal();
}

void StalkModel::gotPacket(NetworkPacket *packet)
{
    if(packet->getPacketType() == 1501)
    {
        emit authSuccessSignal();
//        this->net->sendPacket((new NetworkPacket(2001)));
    }
    else if(packet->getPacketType() == 1502)
    {
        // Disconnect in case connection is still alive
        this->net = NULL;
        emit authFailedSignal();
    }
    else if(packet->getPacketType() == 2501)
    {
//        ContactListResponsePacket *p = (ContactListResponsePacket*) packet;
        emit contactsFullUpdate(packet->decodeJSON());
    }
    else if(packet->getPacketType() == 10501)
    {
        qDebug("Got voice packet");
        this->audioStreamDecoder->play(packet->getBody());
    }
    else
    {
        qDebug() << "StalkModel got unimplemented packet - " << packet->getPacketType();
    }
}

void StalkModel::auth(QString login, QString password)
{
    if(!this->net)
    {
        this->login = login;
        this->password = password;
        this->connectToServer();
    }
}

void StalkModel::setStatus(int statusId)
{
    this->net->sendPacket((new NetworkPacket(1101))->setParam("status_id", statusId));
}

void StalkModel::startVoiceBenchmark()
{
    AudioStreamEncoder *enc = new AudioStreamEncoder();
    connect(enc, SIGNAL(chunkReady(QByteArray)), this, SLOT(sendChunk(QByteArray)));
    enc->startCapture();
}

void StalkModel::sendChunk(QByteArray data)
{
    qDebug() << "STALKMODEL BENCHMARK: Sending data: " << data.length();
    //this->net->sendPacket((new NetworkPacket(10001))->setParam("time", QVariant(this->timer->nsecsElapsed()))->set("data", QVariant(data)));
    this->net->sendPacket((new NetworkPacket(10001))->setBody(data));
}
